package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bean.UserWallet;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.CongziPassActivity;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MywalletonlvieActivity extends Activity implements OnClickListener {

	private TextView mTmoney;
	private TextView mTczhi, mTtixian;
	private String user_money;
	private String user_proceeds;
	private MyApp application;
	private ProgressBar mBar;
	private TextView mTlist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_mywalletonlvie);

		Intent intent = getIntent ();
		user_money = intent.getStringExtra ("user_money");
		user_proceeds = intent.getStringExtra ("user_proceeds");
		application = (MyApp) getApplication ();
		intview ();

	}

	@Override
	protected void onResume() {
		super.onResume ();
		getmywallet ();
	}

	private void intview() {
		findViewById (R.id.avmywalletonlive_image_back).setOnClickListener (this);
		mBar = (ProgressBar) findViewById (R.id.avmyonlive_progre_bar);
		mTmoney = (TextView) findViewById (R.id.avmywa_onlive_money);
		mTczhi = (TextView) findViewById (R.id.avmywa_onlive_chongzhi);
		mTtixian = (TextView) findViewById (R.id.avmywa_onlive_tixian);
		mTlist = (TextView) findViewById (R.id.avmywa_onlive_listlist);//�˵��б�
		mTlist.setOnClickListener (this);
		mTczhi.setOnClickListener (this);
		mTtixian.setOnClickListener (this);
		mTmoney.setText ("￥" + user_money);
	}

	private void getpayid(String platform) {
		if (application.getDatas_load () == null) return;
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (
				MywalletonlvieActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "order");
		params.put ("a", "createorder");
		params.put ("user_id", application.getDatas_load ().getId ());
		params.put ("money", "" + 1);//������1元����Ҫ��
		params.put ("platform", platform);
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
//				 Log.e("�޿�֧��������", "response"+response);
				try {
					String flag = response.getString ("flag");
					if (flag.equals ("success")) {
						JSONObject jsonObject = response.getJSONObject ("data");
						JSONObject json = jsonObject.getJSONObject ("data");
						String order_id = json.getString ("order_id");
						String playform = json.getString ("platform");
						Intent intent = new Intent ();
						intent.setClass (MywalletonlvieActivity.this,
								ChongzhiActivity.class);
						intent.putExtra ("money", 1);
						intent.putExtra ("order_id", order_id);
						intent.putExtra ("playform", playform);
						startActivity (intent);
					} else if (flag.equals ("unknow")) {
						String info = response.getString ("info");
						Toast.makeText (MywalletonlvieActivity.this, info,
								Toast.LENGTH_SHORT).show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (MywalletonlvieActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show ();
				Log.e ("借给他是吧dd", "失败" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();

				mBar.setVisibility (View.VISIBLE);

			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mBar.setVisibility (View.GONE);
//				Log.e("������ǰ�", "���");
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId ()) {
			case R.id.avmywalletonlive_image_back:
				finish ();
				break;

			case R.id.avmywa_onlive_chongzhi:

//			getpayid("nocard");
				Intent intent = new Intent (MywalletonlvieActivity.this, CongziPassActivity.class);
				startActivity (intent);
//			Intent intent = new Intent(MywalletonlvieActivity.this,MywalletonlvieActivity.class);
//			startActivity(intent);
				break;

			case R.id.avmywa_onlive_listlist:

//		getpayid("nocard");
				Intent intent1 = new Intent (MywalletonlvieActivity.this, ZcailistActivity.class);
				startActivity (intent1);
//		Intent intent = new Intent(MywalletonlvieActivity.this,MywalletonlvieActivity.class);
//		startActivity(intent);
				break;

			case R.id.avmywa_onlive_tixian:
				Intent intent2 = new Intent (MywalletonlvieActivity.this, TixianActivity.class);
				startActivity (intent2);
				break;

			default:
				break;
		}
	}


	/***
	 * 我的钱包
	 * 同步数据库用户金额
	 */
	private void getmywallet() {
		Datas_load datasLoad = application.getDatas_load ();
		if (datasLoad != null) {
			String userLogin = datasLoad.getUser_login ();
			if (! TextUtils.isEmpty (userLogin)) {
				HttpMethods.getInstance ().userWallet (mWalletObserver, userLogin);
			}
		}
	}

	private Observer<UserWallet> mWalletObserver = new Observer<UserWallet> () {
		@Override
		public void onSubscribe(Disposable d) {

		}

		@Override
		public void onNext(UserWallet value) {
			if (value != null) {
				String money_f = getString (R.string.user_money_f);
				mTmoney.setText (String.format (money_f, value.getUser_money ()));
				String money_f2 = getString (R.string.user_money_f2);
				user_money = String.format (money_f2, value.getUser_money ());
				user_proceeds = String.format (money_f2, value.getUser_proceeds ());
			}
		}

		@Override
		public void onError(Throwable e) {

		}

		@Override
		public void onComplete() {

		}
	};
}
